package api;


import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class ZKcreate {

    String ip = "azyjz.ltd:2181";
    private ZooKeeper zooKeeper;

    @Before
    public void before() throws Exception{
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        zooKeeper= new ZooKeeper(ip, 50000, new Watcher() {
            public void process(WatchedEvent event) {
                System.out.println("创建连接成功");
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();
    }

    @Test
    public void create1() throws KeeperException, InterruptedException {
        // arg1:节点的路径
        // arg2:节点的数据
        // arg3:权限列表 world:anyone:cdrwa
        // arg4:节点类型 持久化节点
        zooKeeper.create("/create/node1", "node2".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }

    @Test
    public void create2() throws Exception {
        // Ids.READ_ACL_UNSAFE world:anyone:r
        zooKeeper.create("/create/node2", "node2".getBytes(),
                ZooDefs.Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT);
    }

    @Test
    public void create3() throws Exception{
        // world授权模式
        // 权限列表
        List<ACL> acls = new ArrayList<ACL>();
        Id id = new Id("world","anyone");
        acls.add(new ACL(ZooDefs.Perms.READ,id));
        acls.add(new ACL(ZooDefs.Perms.WRITE,id));
        zooKeeper.create("/create/node3","node2".getBytes(),acls,CreateMode.PERSISTENT);
    }

    @Test
    public void create4() throws Exception{
        // ip授权模式
        //权限列表
        ArrayList<ACL> acls = new ArrayList<ACL>();
        Id id = new Id("ip", "117.24.25.220");
        acls.add(new ACL(ZooDefs.Perms.READ,id));
        acls.add(new ACL(ZooDefs.Perms.WRITE,id));
        zooKeeper.create("/create/node4", "node4".getBytes(),
                acls, CreateMode.PERSISTENT);

    }

    @Test
    public void create5() throws Exception {
        // auth授权模式
        // 添加授权用户
        zooKeeper.addAuthInfo("digest", "yongjianz:123456".getBytes());
        zooKeeper.create("/create/node5", "node5".getBytes(),
                ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
    }


    @Test
    public void create6() throws Exception {
        // auth授权模式
        // 添加授权用户
       zooKeeper.addAuthInfo("digest", "yongjianz:123456".getBytes());
        // 权限列表
        List<ACL> acls = new ArrayList<ACL>();
        // 授权模式和授权对象
        Id id = new Id("auth", "yongjianz");
        // 权限设置
        acls.add(new ACL(ZooDefs.Perms.READ, id));
        zooKeeper.create("/create/node6", "node6".getBytes(), acls,
                CreateMode.PERSISTENT);
    }
    @Test
    public void create7() throws Exception {
        // digest授权模式
        // 权限列表
        List<ACL> acls = new ArrayList<ACL>();
        // 授权模式和授权对象
        Id id = new Id("digest", "yongjianz:Q4mtAoh1XckntrNUa0Ul6my+7zk=");
        // 权限设置
        acls.add(new ACL(ZooDefs.Perms.ALL, id));
        zooKeeper.create("/create/node7", "node7".getBytes(), acls,
                CreateMode.PERSISTENT);
    }

    @Test
    public void create8() throws Exception {
        // 持久化顺序节点
        // Ids.OPEN_ACL_UNSAFE world:anyone:cdrwa
        String result = zooKeeper.create("/create/node8",
                "node8".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT_SEQUENTIAL);
        System.out.println(result);
    }

    @Test
    public void create9() throws Exception {
        // 临时节点
        // Ids.OPEN_ACL_UNSAFE world:anyone:cdrwa
        String result = zooKeeper.create("/create/node9",
                "node9".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println(result);
        System.out.println(zooKeeper);
    }

    @Test
    public void create10() throws Exception{
        //创建临时有序节点
        //// Ids.OPEN_ACL_UNSAFE world:anyone:cdrwa
        zooKeeper.create("/create/node10","node10".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL_SEQUENTIAL);
    }

    @Test
    public void create11() throws Exception{
        //异步方式创建节点
        zooKeeper.create("/create/node11", "node11".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL,
                new AsyncCallback.StringCallback() {

                    public void processResult(int rc, String path, Object ctx, String name) {
                        //0 表示创建成功
                        System.out.println(rc);
                        //节点路径，"/create/node11"
                        System.out.println(path);
                        //上下文参数
                        System.out.println(ctx);
                        //节点的路径，/create/node110000000026
                        System.out.println(name);
                    }
                },"im context");
        Thread.sleep(10000);
        System.out.println("结束");
    }

    @After
    public void after() throws InterruptedException {
        zooKeeper.close();
    }
}
